package com.ayoka.ui.html5;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.base.BasePresenter;
import com.ayoka.databinding.ActivityBaseHtml5WebviewBinding;
import com.ayoka.widget.ProgressWebView;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebViewClient;


/**
 * =========================================================
 * @ProjectName:
 * @ClassName: BaseHtml5WebViewActivity
 * @Description: 内置浏览器界面
 * @Author: jincp
 * @CreateDate: 2019/3/14 13:58
 * =========================================================
 */
public abstract class BaseHtml5WebViewActivity extends BaseActivity<ActivityBaseHtml5WebviewBinding, BasePresenter> implements ProgressWebView.ActivityView{

    public static void start(Activity activity, String url, String htmlContent, ToolBarOptions options) {
        Intent intent = getStartIntent(activity, BaseHtml5WebViewActivity.class, url, htmlContent, options);
        activity.startActivity(intent);
    }

    public static Intent getStartIntent(Activity activity, Class clz, String url, String htmlContent, ToolBarOptions options){
        Intent intent = new Intent(activity, clz);
        Bundle bundle = new Bundle();
        bundle.putString("Url", url);
        bundle.putString("Content", htmlContent);
        bundle.putSerializable("Options", options);
        intent.putExtras(bundle);
        return intent;
    }

    protected Bundle bundle;
    protected String url;
    protected String htmlContent;
    protected ToolBarOptions options;

    private ValueCallback<Uri> umUploadMessage;
    private ValueCallback<Uri[]> umUploadMessages;
    private WebChromeClient.FileChooserParams mFileChooserParams;

    @Override
    public int getContentLayout() {
        return R.layout.activity_base_html5_webview;
    }

    @Override
    public void configIntentValue() {
        //得到bundle
        try {
            bundle = getIntent().getExtras();
            if (bundle == null) {
                finish();
                return;
            }
            url = bundle.getString("Url");
            Log.d(TAG, url);
            htmlContent = bundle.getString("Content");
            Log.d(TAG, htmlContent);
            if (TextUtils.isEmpty(url) && TextUtils.isEmpty(htmlContent)) {
                finish();
                return;
            }
            options = (ToolBarOptions) bundle.getSerializable("Options");
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
    }

    @Override
    public void initViews() {
        mDataBinding.webView.setActivityView(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (!TextUtils.isEmpty(url)) {
            //WebView加载web资源
            mDataBinding.webView.loadUrl(url);
        } else {
            mDataBinding.webView.loadDataWithBaseURL(null, buildHtmlContent(htmlContent), "text/html", "utf-8", null);
        }
        if (options == null){
           findViewById(R.id.custom_toolbar).setVisibility(View.GONE);
        } else {
            findViewById(R.id.custom_toolbar).setVisibility(View.VISIBLE);
            setCustomToolBar(mDataBinding.customToolbar.toolbar, options);
        }
    }

    private String buildHtmlContent(String htmlContent) {
        StringBuilder modifiedHtml = new StringBuilder();
        modifiedHtml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">"
                + "<html xmlns=\"http://www.w3.org/1999/xhtml\">" + "<head>" + "<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\"/>"
                + "<meta http-equiv=\"Cache-control\" content=\"public\" />" + "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0\" />"
                + "<link rel=\"stylesheet\" href=\"file:///android_asset/html.css\" type=\"text/css\">");
        modifiedHtml.append("<body>");
        modifiedHtml.append(htmlContent);
        modifiedHtml.append("</body></html>");
        return modifiedHtml.toString();
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void onPause() {
        super.onPause();
        mDataBinding.webView.setOnPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mDataBinding.webView.setOnResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDataBinding.webView.setOnDestroy();
    }


    @Override
    public void onBackPressed() {
        //如果当前浏览器可以后退，则后退上一个页面
        if (mDataBinding.webView.canGoBack()) {
            mDataBinding.webView.goBack();
        } else {
            finish();
        }
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public WebViewClient getWebViewClient() {
        return null;
    }

    @Override
    public WebChromeClient getWebChromeClient() {
        return null;
    }


}
